package com.paisheng.justlife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.paisheng.justlife.R;
import com.paisheng.justlife.bean.Waste;
import com.paisheng.justlife.common.MenuType;

import java.util.List;

public class GvMenuAdapter extends BaseAdapter {

    Context mContext;
    List<Waste> list;
    String deliver;
    Waste waste;
    ViewHolder mHolder;
    private LayoutInflater mlayoutInflater;

    public GvMenuAdapter(Context context, List<Waste> list, String deliver) {
        this.mContext = context;
        this.list = list;
        this.deliver =deliver;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position,  View convertView, ViewGroup viewGroup) {

        if (convertView == null) {
            mHolder = new ViewHolder();
            mlayoutInflater = LayoutInflater.from(mContext);
            convertView = mlayoutInflater.inflate(R.layout.activity_grid_item, null);
            mHolder.img_menu = (ImageView) convertView.findViewById(R.id.img_menu);
            mHolder.tv_menu_name = (TextView) convertView.findViewById(R.id.tv_menu_name);
            mHolder.tv_menu_price = (TextView)convertView.findViewById(R.id.tv_menu_price);
            mHolder.cb_menu_checkbox = (CheckBox) convertView.findViewById(R.id.cb_menu_checkbox);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }

        waste = list.get(position);
        final String name = waste.getName();
        String resultPrice = waste.getResultPrice();
        mHolder.tv_menu_name.setText(name);
        mHolder.cb_menu_checkbox.setChecked(waste.isChecked());
        if(deliver.equals(MenuType.DELIVER)){
            mHolder.tv_menu_price.setText(resultPrice+"/公斤");
            mHolder.cb_menu_checkbox.setVisibility(View.GONE);
        }
        if(deliver.equals(MenuType.RECYCLE)){
            mHolder.tv_menu_price.setText("");
        }

        if (name.equals(MenuType.PLASTIC)) {
            mHolder.img_menu.setBackgroundResource(R.drawable.com_home_ic_plastic);

        }
        if (name.equals(MenuType.METAL)) {
            mHolder.img_menu.setBackgroundResource(R.drawable.com_home_ic_metal);
        }
        if (name.equals(MenuType.CLOTHES)) {
            mHolder.img_menu.setBackgroundResource(R.drawable.com_home_ic_clothes);
        }
        if (name.equals(MenuType.HARMFUL)) {
            mHolder.img_menu.setBackgroundResource(R.drawable.com_home_ic_garbage);
        }
        if (name.equals(MenuType.PAPER)) {
            mHolder.img_menu.setBackgroundResource(R.drawable.com_home_ic_paper);
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView img_menu;
        private TextView tv_menu_name;
        private TextView tv_menu_price;
        private CheckBox cb_menu_checkbox;

    }
}
